package com.qymerge.manage.base.controller;

import com.google.common.reflect.TypeToken;
import com.qymerge.manage.base.dto.MetaOrgDTO;
import com.qymerge.manage.base.dto.MetaProfitReportItemDTO;
import com.qymerge.manage.base.ro.MetaOrgRO;
import com.qymerge.manage.base.ro.MetaProfitReportItemRO;
import com.qymerge.manage.base.ro.MetaProfitTableTemplateRO;
import com.qymerge.manage.base.service.IMetaProfitReportItemService;
import com.qymerge.manage.core.bean.*;
import com.qymerge.manage.core.constant.ParamKey;
import com.qymerge.manage.core.controller.BaseController;
import com.qymerge.manage.core.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title: IMetaProfitReportItemManageController</p>
 * <p>Description: 利润表报表项目配置</p>
 *
 * @author Xiao
 * @date 2025/4/29 17:18
 * @version 1.0.0
 */
@Controller
@RequestMapping(value = "base/profitReportItem")
public class IMetaProfitReportItemManageController extends BaseController {

    @Autowired
    private IMetaProfitReportItemService metaProfitReportItemService;

    /**
     * 列表
     *
     * @param parameters
     * @return
     */
    @RequestMapping(value = "/treeList")
    public void treeList(HttpServletRequest request, HttpServletResponse response,
                            @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                            @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<MetaProfitReportItemRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaProfitReportItemRO>>() {
        }.getType());

        MetaProfitReportItemRO roSearch = paramValue.getData();
        List<MetaProfitReportItemRO> orgROList = metaProfitReportItemService.treeList(roSearch);

        // 封装到 MetaOrgDTO 中
        List<MetaProfitReportItemDTO> dtoList = new ArrayList<MetaProfitReportItemDTO>();
        if (!CollectionUtils.isEmpty(orgROList)) {
            for (MetaProfitReportItemRO ro : orgROList) {
                MetaProfitReportItemDTO dto = new MetaProfitReportItemDTO();
                dto.setId(CommonUtil.getLongNotNull(ro.getId()));
                dto.setName(CommonUtil.getStringNotNullValue(ro.getName()));
                dto.setShowName(CommonUtil.getStringNotNullValue(ro.getShowName()));

                dto.setTemplateId(CommonUtil.getLongNotNull(ro.getTemplateId()));
                dto.setParentId(CommonUtil.getLongNotNull(ro.getParentId()));
                dto.setVisOrder(CommonUtil.getLongNotNull(ro.getVisOrder()));
                dto.setType(CommonUtil.getLongNotNull(ro.getType()));
                dto.setLevel(CommonUtil.getIntegerNotNull(ro.getLevel()));
                dto.setRemark(CommonUtil.getStringNotNullValue(ro.getRemark()));
                dtoList.add(dto);
            }
        }

        PrintCodeUtils.printSuccess(response, MetaProfitReportItemUtils.formatMenuJson(dtoList), callback);
    }

    /**
     * 列表
     *
     * @param parameters
     * @param callback
     */
    @RequestMapping("list")
    public void list(HttpServletRequest request, HttpServletResponse response,

                     @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                     @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<MetaProfitReportItemRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaProfitReportItemRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        MetaProfitReportItemRO ro = paramValue.getData();

        DataTableRO data = metaProfitReportItemService.list(ro, new PageRequest(common.getPage(), common.getSize()));
        PrintCodeUtils.printSuccess(response, data.build(), callback);
    }

    /**
     * 详情
     *
     * @param request
     * @param response
     * @param callback
     * @throws Exception
     */
    @RequestMapping("detail")
    public void detail(HttpServletRequest request, HttpServletResponse response,

                       @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                       @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {

        ParamValue<MetaProfitReportItemRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaProfitReportItemRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        MetaProfitReportItemRO ro = paramValue.getData();

        RequestCheckUtils.checkNotEmpty(ro.getId(), "ID必须");
        MetaProfitReportItemRO resultRO = metaProfitReportItemService.detail(ro.getId());

        PrintCodeUtils.printSuccess(response, resultRO, callback);
    }

    /**
     * 增加
     *
     * @param request
     * @param response
     * @param callback
     */
    @RequestMapping("save")
    public void save(HttpServletRequest request, HttpServletResponse response,

                     @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                     @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {
        ParamValue<MetaProfitReportItemRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaProfitReportItemRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        MetaProfitReportItemRO ro = paramValue.getData();

        ActionResultRO resultRO = metaProfitReportItemService.save(ro);
        PrintCodeUtils.print(response, resultRO, callback);
    }

    /**
     * 删除
     *
     * @param parameters
     * @return
     */
    @RequestMapping(value = "/delete")
    public void delete(HttpServletRequest request, HttpServletResponse response,

                       @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                       @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<MetaProfitReportItemRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaProfitReportItemRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        MetaProfitReportItemRO ro = paramValue.getData();

        ActionResultRO obj = metaProfitReportItemService.delete(ro.getId());
        PrintCodeUtils.print(response, obj, callback);
    }



}
